/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * StoreOpen.java
 *
 * Created on 27/10/2013, 18:20:17
 */
package br.unisinos.simcop.builder.gui;

import br.unisinos.simcop.core.config.ISimcopConfig;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.event.ListDataListener;

/**
 *
 * @author tiago
 */
public class StoreOpen extends javax.swing.JDialog {

    private ISimcopConfig storeSelected;
    private MruList mruList;

    /** Creates new form StoreOpen */
    public StoreOpen(java.awt.Frame parent, ISimcopConfig storeSelected, MruList mruList) {
        super(parent, true);
        initComponents();
        this.mruList = mruList;
        this.storeSelected = storeSelected;
        ParametersTabModel.forTable(tabConfigStoreParameters, storeSelected.getParameters());
        setLocationRelativeTo(null);

        cbRecent.setModel(new RecentModel(mruList));
        cbRecent.updateUI();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabConfigStoreParameters = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        cbRecent = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        btOpen = new javax.swing.JButton();
        btCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setPreferredSize(new java.awt.Dimension(100, 100));
        jPanel1.setLayout(new java.awt.BorderLayout());

        tabConfigStoreParameters.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"", null}
            },
            new String [] {
                "Parameter", "Value"
            }
        ));
        jScrollPane1.setViewportView(tabConfigStoreParameters);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jLabel2.setText("Connection Parameters:");

        jLabel1.setText("Recent:");

        cbRecent.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
        cbRecent.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbRecent.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbRecentItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap(623, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addGap(3, 3, 3))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addComponent(cbRecent, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cbRecent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(21, Short.MAX_VALUE)
                .addComponent(jLabel2))
        );

        jPanel1.add(jPanel3, java.awt.BorderLayout.PAGE_START);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel2.setPreferredSize(new java.awt.Dimension(862, 45));

        btOpen.setText("Open");
        btOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btOpenActionPerformed(evt);
            }
        });
        jPanel2.add(btOpen);

        btCancel.setText("Cancel");
        btCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelActionPerformed(evt);
            }
        });
        jPanel2.add(btCancel);

        getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelActionPerformed
        storeSelected = null;
        dispose();
}//GEN-LAST:event_btCancelActionPerformed

    private void btOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btOpenActionPerformed
        try {
            storeSelected.load(); //load data with the selected configuration parameters
            dispose();
        } catch (Exception ex) {
            Logger.getLogger(StoreOpen.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(this, ex, "Error loading config store", JOptionPane.ERROR_MESSAGE);
        }
}//GEN-LAST:event_btOpenActionPerformed

    private void cbRecentItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbRecentItemStateChanged
        if (storeSelected != null) {            
            try {                
                String storeName = (String) cbRecent.getSelectedItem();                
                storeSelected = mruList.open(storeSelected.getClass().getName(), storeName);
                ParametersTabModel.forTable(tabConfigStoreParameters, storeSelected.getParameters());
                tabConfigStoreParameters.updateUI();
            } catch (Exception ex) {
                Logger.getLogger(StoreOpen.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(this, ex, "Error loading config store", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_cbRecentItemStateChanged

    public ISimcopConfig getStoreSelected() {
        return storeSelected;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCancel;
    private javax.swing.JButton btOpen;
    private javax.swing.JComboBox cbRecent;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabConfigStoreParameters;
    // End of variables declaration//GEN-END:variables

    class RecentModel implements ComboBoxModel {

        MruList mruList;
        List<String> recents;
        String selected;

        public RecentModel(MruList mruList) {
            this.mruList = mruList;
            this.recents = storeSelected != null ? mruList.getList(storeSelected.getClass().getName()) : new ArrayList<String>();
        }

        public void setSelectedItem(Object anItem) {
            this.selected = (String) anItem;
        }

        public Object getSelectedItem() {
            return selected;
        }

        public int getSize() {
            return recents.size() + 1;
        }

        public Object getElementAt(int index) {
            if (index >= 0 && index < recents.size()) {
                return recents.get(index);
            } else {
                return "";
            }
        }

        public void addListDataListener(ListDataListener l) {
        }

        public void removeListDataListener(ListDataListener l) {
        }
    }
}
